import React, { Component } from 'react';
import PropTypes from "prop-types";
import {
    View,
    Text,
    TouchableHighlight,
    StyleSheet,
    Platform
} from 'react-native';

import Color from '../../config/theme/standard/color';
import FontSize from '../../config/theme/standard/fonts';

import {getLabelStyle} from './LabelStyle';
import Variables from '../../config/theme/styles/index';

export default class Label extends Component{

    constructor(props) {
        super(props);
    }

    _onPress(){
        if(this.props.onPress && !this.props.disabled){
            this.props.onPress(this.props.text);
        }
    }

    setNativeProps(nativeProps) {
        this._root.setNativeProps(nativeProps);
    }

    render() {
        let { styles , LabelStyle} = getLabelStyle(Variables);
        const { textAlign, type, color, lineHeight, fontFamily, fontStyle, fontWeight, textDecorationLine, numberOfLines, text, selectable, labelStyle, allowFontScaling, adjustsFontSizeToFit } = this.props;

        const labelStyles = [styles.label];

        let activeOpacity = this.props.disabled ? 1 : 0.8;

        switch (type) {
            case 'header':
                labelStyles.push({fontSize: LabelStyle.headerFontSize});
                break;
            case 'title':
                labelStyles.push({fontSize: FontSize.titleFontSize});
                break;
            case 'secTitle':
                labelStyles.push({fontSize: LabelStyle.secTitleFontSize});
                break;
            case 'button':
                labelStyles.push({fontSize: LabelStyle.buttonFontSize});
                break;
            case 'form':
                labelStyles.push({fontSize: LabelStyle.formFontSize});
                break;
            case 'description':
                labelStyles.push({fontSize: LabelStyle.descriptionFontSize});
                break;
            case 'percent':
                labelStyles.push({fontSize: LabelStyle.percentFontSize});
                break;
            case 'bottom':
                labelStyles.push({fontSize: LabelStyle.bottomFontSize});
                break;
            default:
                break;
        }

        switch (color) {
            case 'description':
                labelStyles.push({color: LabelStyle.descriptionColor});
                break;
            case 'fill':
                labelStyles.push({color: LabelStyle.fillColor});
                break;
            case 'assist':
                labelStyles.push({color:LabelStyle.assistColor});
                break;
            case 'link':
                labelStyles.push({color: LabelStyle.linkColor});
                break;
            case 'error':
                labelStyles.push({color: LabelStyle.errorColor});
                break;
            case 'increase':
                labelStyles.push({color:LabelStyle.increaseColor});
                break;
            case 'decrease':
                labelStyles.push({color:LabelStyle.decreaseColor});
                break;
            case 'navbar':
                labelStyles.push({color: LabelStyle.navbarColor});
                break;
            case 'main':
                labelStyles.push({color: LabelStyle.mainColor});
                break;
            case 'newColor':
                labelStyles.push({color: Color.newColor});
                break;
            case 'normal':
                labelStyles.push({color: LabelStyle.normalColor});
                break;
            case 'disable':
                labelStyles.push({color: LabelStyle.disableColor});
                break;
            case 'press':
                labelStyles.push({color: LabelStyle.pressColor});
                break;
            case 'icon':
                labelStyles.push({color: LabelStyle.iconColor});
                break;
            case 'white':
                labelStyles.push({color: LabelStyle.whiteColor});
                break;
            case 'black':
                labelStyles.push({color: LabelStyle.blackColor});
                break;
            default:
                if(!(typeof color == "null" || typeof color == "undefined" || color.length == 0)){
                    labelStyles.push({color: color});
                }
                break;
        }

        if (textAlign) {
            labelStyles.push({textAlign: textAlign});
        }

        if (lineHeight) {
            labelStyles.push({lineHeight: lineHeight});
        }

        if (fontFamily) {
            labelStyles.push({fontFamily: fontFamily});
        }

        if (fontStyle) {
            labelStyles.push({fontStyle: fontStyle});
        }

        if (fontWeight) {
            labelStyles.push({fontWeight: fontWeight});
        }

        if (textDecorationLine) {
            labelStyles.push({textDecorationLine: textDecorationLine});
        }

        if(Platform.OS=='android' && !allowFontScaling){
            //计算android的font-size的尺寸
            try{
                let  fixedFont=(size)=> {
                    // NOTE: Font Scale should always be the same as the Pixel Ratio on iOS, making this
                    // a no-op.
                    return size * ReactNative.PixelRatio.get() / ReactNative.PixelRatio.getFontScale();
                };

                for(let i=0;i<labelStyles.length;i++){
                    for (let name of Object.keys(labelStyles[i])) {
                        if (name === 'fontSize') {
                            labelStyles[i][name] = fixedFont(labelStyles[i][name])
                        }
                    }
                }

                for(let i=0;i<labelStyle.length;i++){
                    for (let name of Object.keys(labelStyle[i])) {
                        if (name === 'fontSize') {
                            labelStyle[i][name] = fixedFont(labelStyle[i][name])
                        }
                    }
                }
            }catch(e){

            }


        }
       
        if (this.props.onPress) {
            return (
                <TouchableHighlight underlayColor='transparent' activeOpacity={activeOpacity} onPress={this._onPress.bind(this)}>
                    <Text
                        style={ [labelStyles, labelStyle] }
                        numberOfLines={ numberOfLines }
                        selectable={ selectable }
                        allowFontScaling = {allowFontScaling}
                        adjustsFontSizeToFit={adjustsFontSizeToFit}
                    >
                        { text }
                    </Text>
                </TouchableHighlight>
            )
        } else {
            return (
                <View ref={component => this._root = component} {...this.props}>
                    <Text
                        style={[labelStyles, labelStyle]}
                        numberOfLines={numberOfLines}
                        selectable={selectable}
                        allowFontScaling = {allowFontScaling}
                        adjustsFontSizeToFit={adjustsFontSizeToFit}
                    >
                        {text}
                    </Text>
                </View>
            )
        }
    }
}

Label.propTypes ={
    text: PropTypes.string,
    //文字标签内容
    textAlign: PropTypes.oneOf(['left', 'right', 'center']),
    //文字标签对齐方式（居左/居中/居右）
    type: PropTypes.oneOf(['header', 'title', 'secTitle', 'button', 'form', 'description', 'percent', 'bottom']),
    //文字标签类型，控制文本大小
    color: PropTypes.oneOf(['description', 'fill', 'assist', 'link', 'error', 'increase', 'decrease', 'navbar', 'main', 'newColor','disable', 'white', 'normal', 'black', 'press', 'icon']),
    //文本标签颜色
    onPress: PropTypes.func,
    //文本标签点击事件
    lineHeight: PropTypes.number,
    //文本标签行高
    fontFamily: PropTypes.string,
    //文本字体
    fontStyle: PropTypes.oneOf(['normal', 'italic']),
    //文本标签风格（普通/斜体）
    fontWeight: PropTypes.oneOf(['normal', 'bold', '100', '200', '300', '400', '500', '600', '700', '800', '900']),
    //文本粗细
    textDecorationLine: PropTypes.oneOf(['none', 'underline', 'line-through', 'underline line-through']),
    //文字标签是否有下划线（否/是/删除线/下划线和删除线）
    numberOfLines: PropTypes.number,
    //限制本文显示行数，超过限制行数时，以省略号代替
    selectable: PropTypes.bool,
    //文字可以被选择并被复制
    labelStyle: PropTypes.oneOfType([PropTypes.array, PropTypes.object, PropTypes.number]),
    //自定义文字标签样式
    allowFontScaling:PropTypes.bool,
    adjustsFontSizeToFit:PropTypes.bool,
    //指定字体是否随着给定样式的限制而自动缩放
    disabled:PropTypes.bool
};

Label.defaultProps = {
    text: '',
    textAlign: 'left',
    type: 'form',
    color: 'normal',
    fontStyle: 'normal',
    fontWeight: 'normal',
    textDecorationLine: 'none',
    selectable: false,
    numberOfLines: null,
    allowFontScaling:false,
    adjustsFontSizeToFit:false,
    disabled:false
};

